package myky.com.doctorapp.camera;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.listener.ClickListener;
import com.cjt2325.cameralibrary.listener.ErrorListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;
import com.cjt2325.cameralibrary.util.FileUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import myky.com.doctorapp.R;
import myky.com.doctorapp.utils.Constants;
import myky.com.doctorapp.utils.DialogChoice;
import myky.com.doctorapp.utils.ShareUtil;


/**
 *
 * @author Mr.li
 * @date 2018/6/12
 */

public class MedicalTakePhotoActivity extends AppCompatActivity {

    @BindView(R.id.photo_right_menu)
    LinearLayout photo_right_menu;
    @BindView(R.id.takephoto_drawerlayout)
    DrawerLayout takephoto_drawerlayout;
    @BindView(R.id.photo_menu_all)
    LinearLayout photo_menu_all;
    @BindView(R.id.jcameraview)
    JCameraView mJcameraview;
    @BindView(R.id.menu_right_medical01)
    ImageView mMenuRightMedical01;
    @BindView(R.id.menu_right_medical02)
    ImageView mMenuRightMedical02;
    @BindView(R.id.menu_right_medical03)
    ImageView mMenuRightMedical03;
    @BindView(R.id.menu_right_scroll)
    ScrollView mMenuRightScroll;

    private Context mContext;
    private JCameraView jCameraView;
    private String typeName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_medical_takephoto);
        mContext = MedicalTakePhotoActivity.this;
        typeName = getIntent().getStringExtra("TypeName");
        ButterKnife.bind(this);
        initView();
    }


    private void initView() {
        jCameraView = (JCameraView) findViewById(R.id.jcameraview);
        //设置只能录像或只能拍照或两种都可以（默认两种都可以）
        jCameraView.setFeatures(JCameraView.BUTTON_STATE_ONLY_CAPTURE);
        //JCameraView监听
        jCameraView.setErrorLisenter(new ErrorListener() {
            @Override
            public void onError() {
                //打开Camera失败回调
                Log.i("CJT", "open camera error");
            }

            @Override
            public void AudioPermissionError() {
                //没有录取权限回调
                Log.i("CJT", "AudioPermissionError");
            }
        });
        jCameraView.setJCameraLisenter(new JCameraListener() {
            @Override
            public void captureSuccess(Bitmap bitmap) {
                //获取图片bitmapu
                Log.i("JCameraView", "bitmap = " + bitmap.getWidth());
                String path = FileUtil.saveBitmap("Cmai", bitmap);
                Intent intent = new Intent();
                intent.putExtra(Constants.IMAGEPATH, path);
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void recordSuccess(String url, Bitmap firstFrame) {
                //获取视频路径
                Log.i("CJT", "url = " + url);
            }
        });
        //左边按钮点击事件
        jCameraView.setMediaQuality(JCameraView.MEDIA_QUALITY_HIGH);
        jCameraView.setLeftClickListener(new ClickListener() {
            @Override
            public void onClick() {
                finish();
            }
        });
        jCameraView.setCameraDesClickListener(new ClickListener() {
            @Override
            public void onClick() {
                openRightMenu();
            }
        });
        switch (typeName) {
            case "舌苔":
//                jCameraView.hindGridView();
                break;
        }

        photo_menu_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeRightMenu();
            }
        });
        takephoto_drawerlayout.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                takephoto_drawerlayout.getViewTreeObserver().removeOnPreDrawListener(this);
                switch (typeName){
                    case "舌苔":
                        if (ShareUtil.getBooleanTrue("isFirstSheTai")) {
                            openRightMenu();
                        }
                        break;
                    case "影像检查":
                        if (ShareUtil.getBooleanTrue("isFirstYX")) {
                            openRightMenu();
                        }
                        break;
                    default:
                        if (ShareUtil.getBooleanTrue("isFirstQT")) {
                            openRightMenu();
                        }
                        break;
                }

                return false;
            }
        });
        takephoto_drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
                Gravity.RIGHT);
        takephoto_drawerlayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerStateChanged(int newState) {

            }
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }
            @Override
            public void onDrawerOpened(View drawerView) {
                takephoto_drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }
            @Override
            public void onDrawerClosed(View drawerView) {
                switch (typeName){
                    case "舌苔":
                        if (ShareUtil.getBooleanTrue("isFirstSheTai")) {
                            //引导页库和相机冲突，
                            DialogChoice.showJurisdiction_W(mContext, R.layout.set_show_takephoto_guide);
                            ShareUtil.putData("isFirstSheTai", false);
                        }
                        break;
                    case "影像检查":
                        if (ShareUtil.getBooleanTrue("isFirstYX")) {
                            //引导页库和相机冲突，
                            DialogChoice.showJurisdiction_W(mContext, R.layout.set_show_takephoto_guide);
                            ShareUtil.putData("isFirstYX", false);
                        }
                        break;
                    default:
                        if (ShareUtil.getBooleanTrue("isFirstQT")) {
                            //引导页库和相机冲突，
                            DialogChoice.showJurisdiction_W(mContext, R.layout.set_show_takephoto_guide);
                            ShareUtil.putData("isFirstQT", false);
                        }
                        break;
                }
                takephoto_drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
                        Gravity.RIGHT);
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (takephoto_drawerlayout.isDrawerVisible(Gravity.RIGHT)) {
            closeRightMenu();
        } else {
            super.onBackPressed();
        }
    }

    //开启右侧侧滑菜单
    private void openRightMenu() {
        takephoto_drawerlayout.openDrawer(Gravity.RIGHT);
        takephoto_drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,
                Gravity.RIGHT);
        mMenuRightScroll.post(new Runnable() {
            @Override
            public void run() {
                //To change body of implemented methods use File | Settings | File Templates.
//     mRootScrollView.fullScroll(ScrollView.FOCUS_DOWN);
                switch (typeName) {
                    case "舌苔":
                        int offset = mMenuRightMedical01.getHeight() + mMenuRightMedical02.getHeight();
                        mMenuRightScroll.smoothScrollTo(0, offset);
                        break;
                    case "影像检查":

                        break;
                    default:
                        int offset2 = mMenuRightMedical01.getHeight();
                        mMenuRightScroll.smoothScrollTo(0, offset2);
                        break;
                }
            }
        });
    }

    //关闭右边侧滑菜单
    private void closeRightMenu() {
        takephoto_drawerlayout.closeDrawer(Gravity.RIGHT);
        takephoto_drawerlayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,
                Gravity.RIGHT);
    }

    @Override
    protected void onResume() {
        super.onResume();
        jCameraView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        jCameraView.onPause();
    }
}
